$(function () {
    initDatagrid();

    function bindCord() {
        var gatherCodeId = $("#gatherCodeId").val();
        if (gatherCodeId == "" || gatherCodeId == null) {
            parent.$.messager.alert('请输入用户新收分码');
            return;
        }else{
            var str = $.util.getRootPath() + "/weixin/gc/s/";
            if(!$.string.contains(gatherCodeId,str)) {
                gatherCodeId = str + gatherCodeId;
            }
        }

        //var url = $.util.getRequestJson('/json/weixin/gatherCodeHandle.scanning.json');
        $.util.get(gatherCodeId, function (res) {
            $("input[name='gatherCodeId']").val("").focus();
            if(res.statusCode=='200') {
                $('#listTable').datagrid('reload');
            }
            $.easyui.messager.show(res.message);
        });
    }

    //绑定收分码
    $("#btn").click(function () {
        bindCord();
    });
    $('#gatherCodeId').keydown(function (event) {
        if (event.keyCode == 13) {
            bindCord();
        }
    });

});

// 查看收分码
function findDetail(codeId) {
    var title = '查看收分码';
    var url = $.util.getRootPath() + '/page/operatorManager/code/findCode.html';
    url = codeId ? url + '?id=' + codeId : url;
    var dialog = $.easyui.showDialog({
        title: title,
        width: 350,
        height: 350,
        maximizable: true,
        href: url,
        topMost: true,
        enableSaveButton:false,
        closeButtonText: '关闭'
    });
}

// 表格内容初始化
function initDatagrid() {
    var url = $.util.getRequestJson('/json/storeHandle.codeList.json');

    $('#listTable').datagrid({
        url: url,
        idField: 'id',
        fit: true,
        pageSize: 20,
        pagination: true,
        checkOnSelect: false,
        selectOnCheck: false,
        singleSelect: true,
        toolbar: '#toolbarButton',
        fitColumns: true,
        columns: [
            [
                {
                    field: 'id',
                    title: '收分码编号',
                    align: 'center',
                    width: 12
                },
                {
                    field: 'bindTime',
                    title: '绑定时间',
                    align: 'center',
                    width: 12
                },
                {
                    field: 'status',
                    title: '收分码状态',
                    align: 'center',
                    width: 12,
                    formatter: function (value, row) {
                        //return value == null ? "待审核":value;
                        if (value == '1') {
                            return "<font color='green'>已绑定</font>"
                        } else if (value == '2') {
                            return "<font color='red'>已停用</font>"
                        } else {
                            return "已废弃"
                        }
                    }
                },
                {
                    field: 'context',
                    align: 'center',
                    width: 10,
                    title: '操作',
                    formatter: function (value, row, index) {
                        var aEl = '<a href=javascript:findDetail("{0}"); class="easyui-tooltip" title="查看">查看</a>';
                        if (row.status == '1') {
                            aEl += ' | <a href=javascript:stopCode("{0}"); class="easyui-tooltip" title="停用收分码">停用</a>' +
                                   ' | <a href=javascript:changeCode("{0}"); class="easyui-tooltip" title="更换收分码">更换</a>';
                        }else if(row.status == '2'){
                            aEl += ' | <a href=javascript:startCode("{0}"); class="easyui-tooltip" title="启用收分码">启用</a>';
                        }

                        //格式化操作按钮
                        return $.string.format(aEl, row.id,index);
                    }
                }
            ]
        ]
    });
}


// 停用收分码
function stopCode(id) {
    parent.$.messager.confirm('确认对话框', '停用收分码会导致现绑定的收分码失效，是否继续？', function (r) {
        if (r) {
            var url = $.util.getRequestJson('/json/storeHandle.stopCode.json');
            $.util.get(url, {id: id}, function (res) {
                // 停用成功重新加载
                if (res.success) {
                    $('#listTable').datagrid('reload');
                }
                $.easyui.messager.show(res.message);
            });
        }
    });
}

// 启用收分码
function startCode(id) {
    parent.$.messager.confirm('确认对话框', '您确定启用该收分码吗？', function (r) {
        if (r) {
            var url = $.util.getRequestJson('/json/storeHandle.startCode.json');
            $.util.get(url, {id: id}, function (res) {
                // 启用成功重新加载
                if (res.success) {
                    $('#listTable').datagrid('reload');
                }
                $.easyui.messager.show(res.message);
            });
        }
    });
}

// 更换收分码
function changeCode(id) {
    parent.$.messager.confirm('确认对话框', '更换收分码会导致现绑定的收分码失效，是否继续？', function (r) {
        if (r) {
            var title = "更换收分码";
            var url = $.util.getRootPath() + '/page/shopManager/order/changeCode.html';
            url = url + '?id=' + id;
            var dialog = $.easyui.showDialog({
                iconCls: "",
                title: title,
                width: 800,
                height: 435,
                maximizable: true,
                href: url,
                topMost: true,
                saveButtonText: '更换',
                onSave: function () {
                    /*$('#gatherCodeId').keydown(function (event) {
                        if (event.keyCode == 13) {
                            subForm(dialog,id);
                        }
                    });*/
                    subForm(dialog,id);
                }
            });
        }
    });
}

// 更换收分码
function subForm(dialog,id) {
    var gatherCodeId = parent.$("#gatherCodeId").val();
    if (gatherCodeId == "" || gatherCodeId == null) {
        parent.$.messager.alert('请输入用户新收分码');
        return;
    }else{
        var str = $.util.getRootPath() + "/weixin/gc/s/";
        if(!$.string.contains(gatherCodeId,str)) {
            gatherCodeId = str + gatherCodeId;
        }
    }
    $.util.get(gatherCodeId, function (res) {
        parent.$('#gatherCodeId').val("");
        if(res.statusCode=='200') {
            var url = $.util.getRequestJson('/json/storeHandle.boundCode.json');
            $.util.get(url, {id: id}, function (data) {
                if (data.success) {
                    dialog.dialog('close');
                    $('#listTable').datagrid('reload');
                }
                $.easyui.messager.show(data.message);
            });
        }else{
            $.easyui.messager.show(res.message);
        }
    });
}


//绑定收分码
/*function subCode() {
    var gatherCodeId = $("#gatherCodeId").val();
    if (gatherCodeId == "" || gatherCodeId == null) {
        parent.$.messager.alert('请扫描用户新二维码');
        return;
    }
    var url = $.util.getRequestJson('/json/storeHandle.bindCode.json');
    $.util.get(url, {gatherCodeId: gatherCodeId}, function (res) {
        if (res.success) {
            $.easyui.messager.show(res.message);
        }
    });
}*/


